/* Copyright by GD GameDuell GmbH, All Rights Reserved
 * This document is strictly confidential and sole property of GD GameDuell GmbH, Berlin, Germany
 */

package de.gameduell.framework.application {
	import de.gameduell.framework.application.controller.Controller;
	import de.gameduell.framework.application.protocol.Protocol;
	import de.gameduell.framework.asset.AssetGroup;
	import de.gameduell.framework.debug.Debug;
	import de.gameduell.framework.game.cheat.CheatDetection;
	import de.gameduell.framework.util.FlashVars;
	import de.gameduell.net.src.communication.CommunicationEvent;
	
	import flash.display.Stage;
	import flash.system.Capabilities;	

	/**
	 * Contains all basic game features, including a communication, but this class shouldn't be
	 * used directly. Use <b>SingleplayerGame</b> or <b>MultiPlayergame</b> instead which are
	 * more spezialized.
	 * 
	 * @see SingleplayerGame
	 * @see MultiplayerGame
	 */
	public class Game extends OnlineApplication {

		private const GAME_ID:String = "gameid";
		
		private var isReady:Boolean = false;
		protected var slotId:int;
		protected var _version:String = "-1";
		
		/**  
		 * @param aStage    = the stage of the application 
		 * @param aAssets   = comprises the assets from the configfile
		 * @param aSettings = comprises the settings from the configfile
		 * @param isOnline  = is the application online or standalone
		**/	
		override public function init(aStage:Stage, aAssets:AssetGroup, aSettings:Settings, isOnline:Boolean):void {
			super.init(aStage, aAssets, aSettings, isOnline);	
			if(!isStandalone) {
				slotId = extractSlotId();
			}
		}
		
		/**  
		 * extracts the slotid
		**/	
		protected function extractSlotId():int {
			var items:Array = FlashVars.getKeyValue(GAME_ID).split("_");
			return int(items[1]);
		}
		
		/**  
		 * @param aController = the Controller of the current application 
		**/	
		protected function initController(aController:Controller):void {
			aController.slotid = slotId;
			aController.setCommunication(communication);
			aController.setCheatDetection(new CheatDetection(timeline));
			aController.setSettings(settings);
			aController.isStandalone = isStandalone;
		}
		
		/**  
		 * is called if the communication sends the event
		**/
		override protected function onConnected(event:CommunicationEvent):void {
			super.onConnected(event);
			if(isReady){
				sendReady();
			}
		}
		
		/**  
		 * if the connection to the server is established send ready
		**/
		protected function ready():void{
			isReady = true;
			if(isConnected){		
				sendReady();
			}
		}
		
		/**  
		 * send ready to the server
		**/
		protected function sendReady():void{
			Debug.trace("Game::sendReady");
			var operatingSys:String  = Capabilities.os;
			var playerVersion:String = Capabilities.version;
			var playerType:String    = Capabilities.playerType;
			var hasAudio:String      = Capabilities.hasAudio ? "audio" : "noaudio";
			var isDebugger:String    = Capabilities.isDebugger ? "debug" : "nodebug";
			var resolution:String    = Capabilities.screenResolutionX + "x" + Capabilities.screenResolutionY;
			var lang:String          = Capabilities.language;
			var versionString:String = _version + ":" + playerVersion + ":" + playerType + ":" + operatingSys + ":" + hasAudio + ":" + isDebugger + ":" + resolution + ":" + lang;
			if(FlashVars.getKeyValue("avatar") == FlashVars.NO_VALUE){
				communication.send(Protocol.LOADED, []);
			}else{
				communication.send(Protocol.LOADED, [FlashVars.getKeyValue("avatar")]);
			}
			Debug.trace("Version: " + versionString, Debug.DEFAULT);
			communication.send(Protocol.VERSION, [versionString]);
		}
	}
}
